#!/usr/bin/perl

use strict;
use warnings;
use Carp::Clan;
use Data::Dumper;
use lib 'lib';
use Encode;
use Database;

# 統計情報
my $db = Database->new();
my $accounts = $db->account;
while(my $account = $accounts->next){

  # 統計情報取得
  my $data;
  $data->{accountId} = $account->accountId;
  $data->{oldest} = $db->getOldestPostdate($account); # 最古投稿日
  $data->{latest} = $db->getLatestPostdate($account); # 直近投稿日
  $data->{count} = $db->getNumOfTweet($account); # ツイート数

=cut
  # クラスタ分析
  use Text::MeCab;
  use HTML::TreeBuilder;

  # 発言取得
  my $tweets;
  my $threads = $db->thread;
  while(my $thread = $threads->next){
    $tweets .= $thread->tweet;
  }
  my $replies = $db->reply;
  while(my $reply = $replies->next){
    $tweets .= $reply->tweet;
  }
  $tweets =~ s/[\n\r\f]//g if($tweets);

  # 分析
  my $tree = HTML::TreeBuilder->new;
  my $mecab = Text::MeCab->new;
  if($tweets){

    # テキスト化
    my $tree = HTML::TreeBuilder->new;
    $tree->parse($tweets);
    $tweets = $tree->as_text;

    # 形態素解析
    for(my $node = $mecab->parse($tweets); $node; $node = $node->next){
      my $feature = $node->feature;
      my $surface = $node->surface if($node->feature =~ /名詞/);

      # フィルタ
      next unless($surface);
      next if(&_lengthUtf8($surface) == 1);
      print $surface."\n" if($surface);
    }
  }
=cut

  # 更新
  $db->updateUserStatistics($data);
  print encode_utf8($account->name)."\n";
}

my $threads = $db->thread;
while(my $thread = $threads->next){

  # 直近コメント日付
  my $data;
  my $replies = $thread->repliesDesc;
  $data->{threadId}   = $thread->threadId;
  $data->{replyCount} = $replies->count || 0;
  $data->{updated}    = $replies->count == 0 ? undef : $replies->first->posted;

  # 更新
  $db->updateThreadStatistics($data);
  print $thread->threadId."\n";
}

# utf8フラグ付き文字長取得
sub _lengthUtf8{
  my $str = shift;
  if($str){
    return length(Encode::decode_utf8($str));
  } else {
    return 0;
  }
}



